package zhongbo.java.air.flightpath.listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import zhongbo.java.air.common.exception.NoStackLogException;
import zhongbo.java.air.flightpath.config.RedisListenerConfig;
import zhongbo.java.air.flightpath.model.vo.LogVo;

import java.util.Objects;

/**
 * @author WeiJin
 * @version 1.0
 * @since 2025/4/2 9:14
 */
@Component
public class RedisLogChanelPublisher {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 发布消息
     *
     * @param logVo 日志对象
     */
    public void publish(LogVo logVo) {
        try {
            String strLogVo = objectMapper.writeValueAsString(logVo);
            redisTemplate.convertAndSend(RedisListenerConfig.LOG_CHANEL, strLogVo);
        } catch (JsonProcessingException e) {
            throw new NoStackLogException(e);
        }

    }
}
